// -----------------------------------------------------------------------
//  <copyright file="AspNetCorePack.cs" company="OSharp开源团队">
//      Copyright (c) 2014-2018 OSharp. All rights reserved.
//  </copyright>
//  <site>http://www.osharp.org</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2018-06-23 15:22</last-date>
// -----------------------------------------------------------------------


namespace OSharp.AspNetCore;

/// <summary>
/// AspNetCore模块
/// </summary>
[Description("AspNetCore模块")]
public class AspNetCorePack : OsharpPack
{
    /// <summary>
    /// 获取 模块级别
    /// </summary>
    public override PackLevel Level => PackLevel.Core;

    /// <summary>
    /// 获取 模块启动顺序，模块启动的顺序先按级别启动，级别内部再按此顺序启动
    /// </summary>
    public override int Order => 2;

    /// <summary>
    /// 将模块服务添加到依赖注入服务容器中
    /// </summary>
    /// <param name="services">依赖注入服务容器</param>
    /// <returns></returns>
    public override IServiceCollection AddServices(IServiceCollection services)
    {
        services.AddHttpContextAccessor();
        services.TryAddTransient<IHostHttpCrypto, HostHttpCrypto>();

        //注入当前用户，替换Thread.CurrentPrincipal的作用
        services.AddTransient<IPrincipal>(provider =>
        {
            IHttpContextAccessor accessor = provider.GetService<IHttpContextAccessor>();
            return accessor?.HttpContext?.User;
        });

        return services;
    }
}
